﻿using RXY_Mall.Application.User;
using System.Threading.Tasks;
using System.Web.Mvc;
using RXY_Mall.Application.User.Dto;
using RXY_Mall.Application.Statistics;

namespace RXY_Mall.Admin.Controllers
{
    public class UserController : BaseController
    {
        private IUserService _userService;
        private IStatisticsService _statisticsService;
        public UserController()
        {
            _userService = new UserService();
            _statisticsService = new StatisticsService();
        }

        public ActionResult Create()
        {
            UserAddInput input = new UserAddInput();
            return View(input);
        }

        [HttpPost]
        public async Task<ActionResult> Create(UserAddInput input)
        {
            var result = await _userService.AddUser(input);
            return View();
        }

        [HttpGet]
        public ActionResult List()
        {
            ViewBag.SetRole = 0;       
            var statistics = _statisticsService.GetUserStatistics();
            if (statistics==null)
            {
                ViewBag.user_total = 0;
                ViewBag.user_today = 0;
            }
            else
            {
                ViewBag.user_total = statistics.user_total;
                ViewBag.user_today = statistics.user_today;
            }
            return View();
        }

        //public JsonResult GetUsersByPage( int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string userName, string userNo, int status, int grade, int isVip)
        //{
        //    int totalCount = 0;
        //    var userList = _userService.GetUsersByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, userName, userNo, status, grade, isVip, out totalCount);
        //    return Json(new {iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = userList }, JsonRequestBehavior.AllowGet);
        //}

        public JsonResult GetUsersByPage(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string userName, string userNo, int status, int grade)
        {
            var result = _userService.GetUsersByPage(dateMin, dateMax, userName, userNo, status, grade, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new {iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        //重置密码  
        [HttpGet]
        public ActionResult SetPwd(int id)
        {
            if (id <= 0)
            {
                return Json(new { status = "n", info = "参数错误。" });
            }
            //var result = userService.SetUserPwd(MemberId, id, "e10adc3949ba59abbe56e057f20f883e");
            var result = _userService.SetUserPwd(MemberId, id, "123456");
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }

        //禁用/启用
        [HttpPost]
        public ActionResult SetIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) ||! int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _userService.SetIsUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }

        //设为趣卡会员
        [HttpPost]
        public async Task<ActionResult> SetVip()
        {
            if (MemberId == 1 || MemberId == 47)
            {
                string id = Request.Form["id"];
                string isVip = Request.Form["isVip"];
                int idDefault = 0;
                int isVipDefault = 0;
                if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
                {
                    return Json(new { status = "n", info = "ID错误" });
                }
                if (string.IsNullOrWhiteSpace(isVip) || !int.TryParse(isVip, out isVipDefault))
                {
                    return Json(new { status = "n", info = "参数错误" });
                }

                var result = await _userService.SetVip(MemberId, idDefault, isVipDefault);
                if (result)
                {
                    return Json(new { status = "y", info = "设置成功。" });
                }
                else
                {
                    return Json(new { status = "n", info = "设置失败。" });
                }
            }
            else
            {
                return Json(new { status = "n", info = "没有权限！" });
            }
        }
    }
}